package org.geektimes.projects.user.subscriber;

import org.geekbang.projects.injection.Component;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
* 测试主动取消订阅
* @author : lsq
*/
@Component
public class TestCancelSubscriber implements Subscriber {

	private String name = TestCancelSubscriber.class.getSimpleName();

	private static final Logger logger = Logger.getLogger(TestCancelSubscriber.class.getName());

	private Subscription subscription;


	@Override
	public void onSubscribe(Subscription subscription) {
		this.subscription = subscription;
	}

	@Override
	public void onNext(Object o) {
		logger.log(Level.INFO , String.format("%s收到订阅消息%s" ,name, o.toString()));
		throw new RuntimeException("主动取消");
	}

	@Override
	public void onError(Throwable t) {
		logger.log(Level.INFO , String.format("%s订阅异常 %s" , name , t.getMessage()));
		///tODO
		subscription.cancel();
	}

	@Override
	public void onComplete() {
		logger.log(Level.INFO , String.format("%s订阅完成" , name));
	}
}
